//
//  MoreViewController.m
//  TingLiFeng
//
//  Created by xinxuan on 16/1/3.
//  Copyright © 2016年 xinxuan. All rights reserved.
//

#import "MoreViewController.h"
#import "TrainViewController.h"
#import "CustomViewController.h"
#import "PlayerViewController.h"
#import "NavigationViewController.h"
#import "LFDBManager.h"
@interface MoreViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView * tableView;

@property (nonatomic, strong) NSMutableArray *modelsArray;

@property (nonatomic,assign)int  useNumber;//使用的次数
@property (nonatomic,strong)NSString * lastTime;//上次使用的时间
@property (nonatomic,assign)int  cycle;//完成的护理数
@property (nonatomic,strong)NavigationViewController * navigation;
@end

@implementation MoreViewController

static NSString *reuseID = @"cell1";


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.name;
    [self getModelWithTitle:self.name];
  
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    
    [self.view addSubview:self.tableView];
    
}




-(void)getModelWithTitle:(NSString*)title{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
    NSArray *array = [dic objectForKey:title];
    _modelsArray = [NSMutableArray array];
    
    for (NSDictionary *dict in array) {
        CommenModel * model0 =[[LFDBManager shareManager]fetchDataWithName:dict[@"name"]];

        CommenModel *model = [CommenModel new];
        model.pic = dict[@"pic"];
        model.name = dict[@"name"];
        model.centent = dict[@"centent"];
        model.growth = dict[@"growth"];

        model.starCount = dict[@"starCount"];
        model.level = dict[@"level"];
        
        if ([[LFDBManager shareManager]selectNewsTitle:dict[@"name"]]) {
            
            model.useNumber = model0.useNumber;
            
           NSDate *  senddate=[NSDate date];
            
            model.lastTime = senddate;
            
            model.cycle = model0.cycle;
            
        }else{
        
            model.useNumber = 1;
            
            NSDate *  senddate=[NSDate date];
            
            
            model.lastTime = senddate;
            
            model.cycle = 0;
        }

       
        model.period = dict[@"period"];
        model.personNumber = dict[@"personNumber"];
        model.time = dict[@"time"];
        
        [self.modelsArray addObject:model];
        
    
    }
}




#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    BGTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    CommenModel * model = self.modelsArray[indexPath.row];
    cell.background.image = [UIImage imageNamed:model.pic];
    cell.nameLabel.text = model.name;
    cell.contentLabel.text = model.centent;
    cell.levelLabel.text = model.level;
    [cell.starView addStarViewWithCount:[model.starCount intValue]];
    

    if ([[LFDBManager shareManager]selectNewsTitle:model.name]) {
        
        NSString * periodString = [NSString stringWithFormat:@"已使用%lu次 ",(unsigned long)model.useNumber];
        cell.dateLabel.text = periodString;
        
       // NSDate *  senddate=[NSDate date];
        
        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        
        [dateformatter setDateFormat:@"MM/dd  hh:mm"];
        
        NSString *  locationString=[dateformatter stringFromDate:model.lastTime];
        NSString * personNumberString = [NSString stringWithFormat:@"上一次训练  %@",locationString];
        cell.numberLabel.text = personNumberString;
        
        
        NSString * timeString = [NSString stringWithFormat:@"完成%lu次护理",(unsigned long)model.cycle];
        cell.timeLabel.text = timeString;
        
        
    }else{
        NSString * periodString = [NSString stringWithFormat:@"%@天/丰胸",model.period];
        cell.dateLabel.text = periodString;
        
        NSString * personNumberString = [NSString stringWithFormat:@"%@人在练",model.personNumber];
        cell.numberLabel.text = personNumberString;
        
        NSString * timeString = [NSString stringWithFormat:@"约%@分钟",model.time];
        cell.timeLabel.text = timeString;
        
        
        
        
    }

    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    CommenModel * model = [CommenModel new];
    model = _modelsArray[indexPath.row];
    
    if (indexPath.row == 4) {

        CustomViewController * moreVc = [[CustomViewController alloc] init];
        moreVc.name = model.name;
        [self.navigationController pushViewController:moreVc animated:YES];
    }else{
        
        NSString *string1 = model.name;
        
        NSString *string2 = [string1 substringWithRange:NSMakeRange(0, 2)];
        if ([string2 isEqualToString:@"音乐"]) {
            PlayerViewController * playVC = [[PlayerViewController alloc]init];
            [self.navigationController pushViewController:playVC animated:YES];
        }else{

        TrainViewController * trainVc = [[TrainViewController alloc] init];
            trainVc.model = model;
        trainVc.nameString = model.name;
        trainVc.detailString = model.centent;
        trainVc.totalTime = [model.time integerValue] * 60;
        [self.navigationController pushViewController:trainVc animated:YES];
        }
    }
    
    
}






- (void)setupStarImages
{
    
    
   }


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (Screen_Width == 320 && Screen_Height == 480) {//苹果4
        
        return 130;
        
    }else if(Screen_Width == 320 && Screen_Height == 568){//苹果5
        
        return 130;

        
    }else if(Screen_Width == 375 && Screen_Height == 667){//苹果6
        
        return 150;

        
    }else{//苹果6puls及其以上
        
        return 160;

    }
    
    
}



#pragma mark ---- 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        NSLog(@"%lu",(unsigned long)self.inNumber);
        if (self.inNumber >1) {
            _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - 50  - 20 )];

        }else{
            _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, Screen_Width, Screen_Height - 50  - 20 - 40)];

        
        }
        

       
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        [_tableView registerNib:[UINib nibWithNibName:@"BGTableViewCell" bundle:nil] forCellReuseIdentifier:reuseID];
    }
    return _tableView;
}


@end
